package com.zhf.eduuserboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhf.eduuserboot.entity.User;
import com.zhf.eduuserboot.mapper.UserMapper;
import com.zhf.eduuserboot.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2021/2/14 13:10
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public User login(String phone, String password) {
        //创建条件构造器
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        //编写具体的查询条件
        wrapper.eq("phone", phone);
        wrapper.eq("password", password);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public Integer checkPhone(String phone) {
        //创建条件构造器
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("phone", phone);
        return userMapper.selectOne(wrapper)==null ? 0 : 1 ;
    }

    @Override
    public Integer register(String phone, String password, String nickName, String headImg) {
        User user = new User();
        user.setPhone(phone);
        user.setPassword(password);
        user.setPortrait(headImg);
        user.setName(nickName);
        userMapper.insert(user);
        return null;
    }

    @Override
    public Integer updatePortraitAndName(String userid, String portrait, String name) {
        User user = new User();
        user.setId(Integer.parseInt(userid));
        user.setPortrait(portrait);
        user.setName(name);
        userMapper.updateById(user);
        return null;
    }

    @Override
    public Integer updatePassword(String userid, String password) {
        User user = new User();
        user.setId(Integer.parseInt(userid));
        user.setPassword(password);
        userMapper.updateById(user);
        return null;
    }
}
